﻿using AdminSystem.Application.Commands.BreedCommands;
using AdminSystem.Domain.Entities;
using AdminSystem.EntityFramworkCore;
using AdminSystem.Repositories.Interfaces;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminSystem.Application.CommandHandlers.BreedCommandHandlers
{
    public class BreedUpdCommandHandler : IRequestHandler<BreedUpdCommand, APIResult<object>>
    {
        private readonly IBaseRepositories<Breed> breedBase;
        public BreedUpdCommandHandler(IBaseRepositories<Breed> breedBase)
        {
            this.breedBase = breedBase;
        }
        public async Task<APIResult<object>> Handle(BreedUpdCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Message = "修改成功";
            result.Codes = APIState.Success;
            Breed breed=await breedBase.GetById(request.BreedId);
            if (breed == null)
            {
                result.Message = "养殖信息不存在";
                result.Codes = APIState.NotFound;
                return await Task.FromResult(result);
            }
            else
            {
                breed.Variety = request.Variety;
                breed.Source = request.Source;
                breed.FeedingDays = request.FeedingDays;
                breed.FeedingBrand = request.FeedingBrand;
                breed.FeedingName = request.FeedingName;
                breed.SaleDate = request.SaleDate;
                await breedBase.Update(breed);
            }
                return await Task.FromResult(result);
        }
    }
}
